package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import javax.xml.datatype.DatatypeFactory;  
import javax.xml.datatype.XMLGregorianCalendar;  
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import com.lb.jwitter.service.ConditionReport; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.util.Message; 
import java.util.Collections; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.List; 
import java.util.Map; 
import com.lb.jwitter.util.ApplicationContextProvider; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.lb.jwitter.service.TblDKMHKyHieu;
import com.lb.jwitter.service.TblDKMHKyHieuService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHKyHieuAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private String idList; 
    public String getIdList() { return idList; } 
    public void setIdList(String idList) { this.idList = idList; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }

    private String kyHieu; 
    public void setKyHieu(String kyHieu) {
       this.kyHieu = kyHieu;
 }

    public String getKyHieu() {
       return kyHieu;
 }

    private String dienGiai; 
    public void setDienGiai(String dienGiai) {
       this.dienGiai = dienGiai;
 }

    public String getDienGiai() {
       return dienGiai;
 }

    private String nhomKyHieu; 
    public void setNhomKyHieu(String nhomKyHieu) {
       this.nhomKyHieu = nhomKyHieu;
 }

    public String getNhomKyHieu() {
       return nhomKyHieu;
 }

    private TblDKMHKyHieu tblDKMHKyHieu; 
    public TblDKMHKyHieu getTblDKMHKyHieu () {
        return this.tblDKMHKyHieu ;
 }

    private Collection<TblDKMHKyHieu> tblDKMHKyHieus; 
    public Collection<TblDKMHKyHieu> getTblDKMHKyHieus() {
        return this.tblDKMHKyHieus; 
      } 

    private TblDKMHKyHieuService tblDKMHKyHieuService; 

	private int preUpdate(Message msg, TblDKMHKyHieu obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblDKMHKyHieu obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String update() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_KyHieu", "sua")){ 
        return "noprivilege"; 
    } 
           tblDKMHKyHieuService = (TblDKMHKyHieuService)context.getBean("tbl_DKMH_KyHieuClient"); 
        msg = new Message(); 
        tblDKMHKyHieu = tblDKMHKyHieuService.gettblDKMHKyHieu(new Long(id)); 
        tblDKMHKyHieu.setKyHieu(kyHieu); 
        tblDKMHKyHieu.setDienGiai(dienGiai); 
        tblDKMHKyHieu.setNhomKyHieu(nhomKyHieu); 
        preUpdate(msg,tblDKMHKyHieu); 
       if (msg.isPreMessage_b()) 
       { 
           tblDKMHKyHieuService.updatetblDKMHKyHieu(tblDKMHKyHieu); 
           postUpdate(msg, tblDKMHKyHieu); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblDKMHKyHieus =       tblDKMHKyHieuService.findAlltblDKMHKyHieu(pageSize, page); 
      pages = tblDKMHKyHieuService.counttblDKMHKyHieuByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&kyHieu="; 
      dataString += "&dienGiai="; 
      dataString += "&nhomKyHieu="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_DKMH_KyHieu Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_KyHieu", "xem")){ 
        return "noprivilege"; 
    } 
           tblDKMHKyHieuService = (TblDKMHKyHieuService)context.getBean("tbl_DKMH_KyHieuClient"); 
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        tblDKMHKyHieus =        tblDKMHKyHieuService.findAlltblDKMHKyHieu(pageSize, page); 
        pages = tblDKMHKyHieuService.counttblDKMHKyHieuByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&kyHieu="; 
      dataString += "&dienGiai="; 
      dataString += "&nhomKyHieu="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_DKMH_KyHieu Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
  public String delete() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_KyHieu", "xoa")){ 
        return "noprivilege"; 
    }           tblDKMHKyHieuService = (TblDKMHKyHieuService)context.getBean("tbl_DKMH_KyHieuClient"); 
    msg = new Message(); 

    List<String> ids = new ArrayList<String>(); 
    Collections.addAll(ids, idList.split(",")); 
    for(String id : ids){ 
        tblDKMHKyHieuService.deleteListtblDKMHKyHieu(ids); 
    } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblDKMHKyHieus =       tblDKMHKyHieuService.findAlltblDKMHKyHieu(pageSize, page); 
      pages = tblDKMHKyHieuService.counttblDKMHKyHieuByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&kyHieu="; 
      dataString += "&dienGiai="; 
      dataString += "&nhomKyHieu="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Delete tbl_DKMH_KyHieu Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 	private int preInsert(Message msg, TblDKMHKyHieu obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHKyHieu obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String insert() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_KyHieu", "them")){ 
        return "noprivilege"; 
    }           tblDKMHKyHieuService = (TblDKMHKyHieuService)context.getBean("tbl_DKMH_KyHieuClient"); 
        msg = new Message(); 
        tblDKMHKyHieu = new TblDKMHKyHieu(); 
        tblDKMHKyHieu.setKyHieu(kyHieu); 
        tblDKMHKyHieu.setDienGiai(dienGiai); 
        tblDKMHKyHieu.setNhomKyHieu(nhomKyHieu); 
        preInsert(msg,tblDKMHKyHieu); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblDKMHKyHieuService.createtblDKMHKyHieu(tblDKMHKyHieu); 
           postInsert(msg, tblDKMHKyHieu); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblDKMHKyHieu.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblDKMHKyHieus =       tblDKMHKyHieuService.findAlltblDKMHKyHieu(pageSize, page); 
      pages = tblDKMHKyHieuService.counttblDKMHKyHieuByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&kyHieu="; 
      dataString += "&dienGiai="; 
      dataString += "&nhomKyHieu="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_KyHieu Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String search() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_KyHieu", "xem")){ 
        return "noprivilege"; 
    } 
           tblDKMHKyHieuService = (TblDKMHKyHieuService)context.getBean("tbl_DKMH_KyHieuClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(id!= null && !id.equals(0)){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("id");
        con.setValue1(new Long(id));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(kyHieu != null && !kyHieu.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("kyHieu");
        con.setValue1("%" + kyHieu + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(dienGiai != null && !dienGiai.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("dienGiai");
        con.setValue1("%" + dienGiai + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(nhomKyHieu != null && !nhomKyHieu.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("nhomKyHieu");
        con.setValue1("%" + nhomKyHieu + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblDKMHKyHieus = tblDKMHKyHieuService.findtblDKMHKyHieuByCondition(cons,pageSize,page); 
           pages = tblDKMHKyHieuService.counttblDKMHKyHieuByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&id=" + id; 
      dataString += "&kyHieu=" + kyHieu; 
      dataString += "&dienGiai=" + dienGiai; 
      dataString += "&nhomKyHieu=" + nhomKyHieu; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_DKMH_KyHieu Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String editForm() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_KyHieu", "sua")){ 
        return "noprivilege"; 
    } 
           tblDKMHKyHieuService = (TblDKMHKyHieuService)context.getBean("tbl_DKMH_KyHieuClient"); 
        tblDKMHKyHieu = tblDKMHKyHieuService.gettblDKMHKyHieu(new Long(id)); 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_DKMH_KyHieu Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
